子图与一个Python的绘图相对应的柱状图 您所在的位置:网站首页 python 地图 柱状图 子图与一个Python的绘图相对应的柱状图

子图与一个Python的绘图相对应的柱状图

2022-05-03 09:44| 来源: 网络整理| 查看: 265

虽然我在评论中链接的答案提供了这个问题的基本解决方案,但当图形的纵横比“太小”时,可能会出现问题。在这种情况下,即使yticks和ylim是同步的,但是基图和直方图的高度并不是同步的,因为这两个子图的纵横比不同。解决这个问题最简单的方法是使用轴分隔符,而不是通常的add_subplot()方法,正如在this answer的最后一个示例中所做的那样。在

与我之前的suggested solution共享两个图之间的yticks相关,实际上可以得到非常好的结果。为了获得最佳效果,我建议不要使用basemaps colorbar函数,而是直接使用fig.colorbar和颜色条的专用轴。另外,如果只在柱状图的左边显示ytick标签,并将它们隐藏在basemap旁边(解决方案来自here),它看起来(在我看来)最好。如果不需要这样做,可以使用pad关键字在divider.append_axes()中调整直方图和底图之间的距离。在

import numpy as np from matplotlib import pyplot as plt from mpl_toolkits.basemap import Basemap from mpl_toolkits.axes_grid1 import make_axes_locatable data = np.zeros((5000,3)) data[:,0] = np.random.normal(loc=20, scale = 30, size=(5000,)) data[:,1] = np.random.normal(loc=50, scale=10, size=(5000,)) data[:,2] =np.random.uniform(low=0,high=100000,size=(5000,)) ##create a figure with just the main axes: fig, main_ax = plt.subplots() m = Basemap( projection = 'cyl', llcrnrlat = -60., urcrnrlat = 90., llcrnrlon = -180., urcrnrlon = 180., resolution ='l', ax=main_ax, ) x, y =m(data[:,0], data[:,1]) cls = m.scatter( x, y, marker='.', s = 1, c = data[:,2], cmap = 'hot_r', edgecolor = 'none' ) m.fillcontinents(color='grey', lake_color=None, ax=None, alpha=0.1) lats=np.arange(-60.,90.,10) lons=np.arange(-180.,180.,60) ##parallels without labels m.drawparallels(lats, labels =[False, False, False, False], linewidth=0.1) m.drawmeridians(lons,labels =[False, False, False, True], linewidth=0.1 ) ##generating the other axes instances: ##if you want labels at the left side of the map, ##adjust pad to make them visible divider = make_axes_locatable(main_ax) y_hist = divider.append_axes('left', size='20%', pad='5%', sharey=main_ax) cax = divider.append_axes('right',size=0.1,pad=0.1) ##use fig.colorbar instead of m.colorbar fig.colorbar(cls, cax = cax) ## histogram on the attached axes y_hist.hist(data[:,1], 150, histtype='stepfilled', orientation='horizontal', color='blue',alpha=0.2) y_hist.invert_xaxis() ##the y-ticklabels: _,yticks_data = m(0*lats,lats) y_hist.set_yticks(yticks_data) y_hist.set_yticklabels(['{: >3}$^\circ${}'.format( abs(int(y)), 'N' if y>0 else 'S' if y


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有